<template>
	<div class="home-container layout-pd">
		<el-row :gutter="15" class="home-card-two mb15" v-loading=state.global.loading>
			<!-- 左边 -->
			<el-col :xs="24" :sm="10" :md="10" :lg="4" :xl="4" class="home-media">
				<div class="home-card-item"
					style="height: 165px;background: linear-gradient(to right ,#fff 0%, #E4F2FB 100%);">
					<div class="home-card-item-title" style="margin-bottom: 20px;font-size: 18px;">项目总览</div>
					<div style="display: flex;flex-wrap: wrap;justify-content: space-between;">
						<div style="display: flex;flex-direction: column;align-items: center;width: 30%;">
							<div style="font-weight: bold;padding-bottom: 5px;">总人数</div>
							<div style="font-size: 22px;font-weight: bold;margin-top: 5px;">{{ stateAll.leftOne.total }}
							</div>
						</div>
						<div style="display: flex;flex-direction: column;align-items: center;width: 30%;">
							<div style="font-weight: bold;padding-bottom: 5px;">在岗人数</div>
							<div style="font-size: 22px;font-weight: bold;margin-top: 5px;">{{ stateAll.leftOne.normal }}
							</div>
						</div>
						<div style="display: flex;flex-direction: column;align-items: center;width: 30%;">
							<div style="font-weight: bold;padding-bottom: 5px;">休假人数</div>
							<div style="font-size: 22px;font-weight: bold;margin-top: 5px;">{{ stateAll.leftOne.leave }}
							</div>
						</div>
					</div>
				</div>
				<div class="home-card-item" style="margin-top: 15px;height: 382px;">
					<!-- 左二 -->
					<div ref="lineLeftTwoChartRef" style="height: 100%"></div>
				</div>
				<div class="home-card-item" style="margin-top: 15px;height: 320px;">
					<!-- 左三 -->
					<div style="height: 100%" ref="lineLeftFourChartRef"></div>
				</div>
				<div class="home-card-item" style="margin-top: 15px;height: 320px;">
					<!-- 左四 -->
					<div style="height: 100%" ref="homePieRef"></div>
				</div>
			</el-col>

			<!-- 中间 -->
			<el-col :xs="24" :sm="14" :md="14" :lg="16" :xl="16">
				<div class="home-card-item" style="height: 300px;">
					<div class="home-card-item-title">昨日基础数据</div>
					<div class="home-monitor">
						<div class="flex-warp">
							<div class="flex-warp-item" v-for="(v, k) in state.homeThree" :key="k">
								<div class="flex-warp-item-box" :class="`home-animation${k}`">
									<div class="flex-margin">
										<i :class="v.icon" :style="{ color: v.iconColor }"></i>
										<span class="pl5 c-title">{{ v.label }}</span>
										<div class="mt10 c-content">{{ v.value }}</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="home-card-item" style="margin-top: 15px;">
					<!-- 中二 -->
					<div style="height: 100%" ref="homeLineRef"></div>
				</div>
				<div class="home-card-item" style="margin-top: 15px;height: 500px;">
					<!-- 中三 -->
					<div style="height: 100%" ref="lineCenterThreeChartRef"></div>
				</div>
			</el-col>

			<!-- 右边 -->
			<el-col :xs="24" :sm="10" :md="10" :lg="4" :xl="4" class="home-media">
				<div class="home-card-item">
					<!-- 右一 -->
					<div ref="lineRightOneChartRef" style="height: 100%;"></div>
				</div>
				<div class="home-card-item" style="margin-top: 15px;">
					<!-- 右二 -->
					<div ref="lineRightTwoChartRef" style="height: 100%"></div>
				</div>
				<div class="home-card-item" style="margin-top: 15px;">
					<!-- 右三 -->
					<div ref="lineRightThreeChartRef" style="height: 100%"></div>
				</div>

			</el-col>
		</el-row>
	</div>
</template>

<script setup lang="ts" name="home">
import { reactive, onMounted, ref, watch, nextTick, onActivated, markRaw } from 'vue';
import * as echarts from 'echarts';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
import { useHomeAccessInoutSummaryApi, useHomeAttendPointSummaryApi, useHomeAttendYesterdaySummaryApi, useHomeDeviceModuleSummaryApi, useHomeDeviceStateSummaryApi, useHomeEmployeeSummaryApi, useHomePosPointSummaryApi } from '/@/api/home/home';
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';

// 定义变量内容
const homeLineRef = ref();
const homePieRef = ref();
const lineCenterThreeChartRef = ref();
const lineLeftTwoChartRef = ref();
const lineLeftFourChartRef = ref();
const lineRightOneChartRef = ref();
const lineRightTwoChartRef = ref();
const lineRightThreeChartRef = ref();

const storesTagsViewRoutes = useTagsViewRoutes();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const { isTagsViewCurrenFull } = storeToRefs(storesTagsViewRoutes);

const stateAll = reactive({
	leftOne: 0 as any,
	leftTwo: null as any,
	leftThree: null as any,
	leftFour: null as any,
	cenetOne: null as any,
	cenetTwo: null as any,
	cenetThree: null as any,
	rightOne: null as any,
	rightTwo: null as any,
	rightThree: null as any,
})

const echartsMap = {
	offline: '离线',
	online: '在线',
	access: '门禁',
	ladder: '电梯',
	pos: '消费'
} as any

const getName = (data: any) => {
	let nameArr = [] as any
	Object.keys(data).forEach((item) => {
		nameArr.push(echartsMap[item])
	})
	return nameArr
}
const getData = (data: any) => {
	let DataArr = [] as any
	Object.keys(data).forEach((item) => {
		DataArr.push(data[item])
	})
	return DataArr
}

const state = reactive({
	global: {
		loading: false,
		homeChartOne: null,
		homeChartTwo: null,
		homeCharThree: null,
		lineLeftTwoChart: null,
		lineCenterThreeChart: null,
		lineRightOneChart: null,
		lineRightTwoChart: null,
		lineRightThreeChart: null,
		lineLeftFourChart: null,
		dispose: [null, '', undefined],
		allAttendPoints: null
	} as any,
	homeThree: [
		{
			icon: 'iconfont icon-zaosheng',
			label: '迟到总人数',
			value: 10,
			title: 'lossSignIn',
			iconColor: '#F72B3F',
		},
		{
			icon: 'iconfont icon-zaosheng',
			label: '早退总人数',
			value: 10,
			title: 'lossSignOut',
			iconColor: '#91BFF8',
		},
		{
			icon: 'iconfont icon-zaosheng',
			label: '旷工总人数',
			value: 10,
			title: 'lossWork',
			iconColor: '#88D565',
		},
		{
			icon: 'iconfont icon-zaosheng',
			label: '考勤异常总人数',
			value: 10,
			title: 'workStatus',
			iconColor: '#FBD4A0',
		},
		{
			icon: 'iconfont icon-zaosheng',
			label: '消费人数',
			value: '200',
			iconColor: '#F72B3F',
		},
		{
			icon: 'iconfont icon-zaosheng',
			label: '消费人数',
			value: '200',
			iconColor: '#91BFF8',
		},
		{
			icon: 'iconfont icon-zaosheng',
			label: '消费人数',
			value: '200',
			iconColor: '#88D565',
		},
		{
			icon: 'iconfont icon-zaosheng',
			label: '消费人数',
			value: '200',
			iconColor: '#FBD4A0',
		}
	],
	myCharts: [] as any,
	charts: {
		theme: '',
		bgColor: '',
		color: '#303133',
	},
});
const initLeftOne = () => {
	state.global.loading = true;
	useHomeEmployeeSummaryApi().then(({ content }) => {
		state.global.loading = false;
		stateAll.leftOne = content;
	})
}

// 左2
const initLineLeftTwoChart = () => {
	if (!state.global.dispose.some((b: any) => b === state.global.lineLeftTwoChart)) state.global.lineLeftTwoChart.dispose();
	state.global.lineLeftTwoChart = markRaw(echarts.init(lineLeftTwoChartRef.value, state.charts.theme));
	const option = {
		title: {
			text: '进出统计',
			x: 'left',
			textStyle: { fontSize: '15', color: state.charts.color },
		},
		grid: {
			top: 50,
			right: 10,
			bottom: 40,
			left: 30,
		},
		tooltip: {
			trigger: 'axis',
		},
		xAxis: {
			type: 'category',
			boundaryGap: false,
			data: ['1月', '2月', '3月', '4月', '5月'],
			axisLine: {
				lineStyle: {
					color: 'rgba(22, 207, 208, 0.1)',
					width: 1,
				},
			},
			axisTick: {
				show: false,
			},
			axisLabel: {
				interval: 0,
				color: '#16cfd0',
				textStyle: {
					fontSize: 10,
				},
			},
		},
		yAxis: [
			{
				type: 'value',
				boundaryGap: true,
				axisLabel: {
					color: '#16cfd0',
				},
				splitLine: {
					show: true,
					lineStyle: {
						color: 'rgba(22, 207, 208, 0.3)',
					},
				},
				splitArea: {
					show: true,
					areaStyle: {
						color: 'rgba(22, 207, 208, 0.02)',
					},
				},
				nameTextStyle: {
					color: '#16cfd0',
				},
			},
		],
		series: [
			{
				name: '离开人数',
				type: 'line',
				stack: '总量',
				smooth: true,
				lineStyle: {
					width: 0,
				},
				areaStyle: {
					opacity: 0.8,
					color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
						{
							offset: 0,
							color: 'rgba(7,165,255,0.2)',
						},
						{
							offset: 1,
							color: 'rgba(7,165,255,0.7)',
						},
					]),
				},
				emphasis: {
					focus: 'series',
				},
				data: [14, 22, 10, 26, 9],
			},
			{
				name: '天数',
				type: 'line',
				stack: '总量',
				smooth: true,
				lineStyle: {
					width: 0,
				},
				areaStyle: {
					opacity: 0.8,
					color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
						{
							offset: 0,
							color: 'rgba(7,165,255,0.2)',
						},
						{
							offset: 1,
							color: 'rgba(7,165,255,0.7)',
						},
					]),
				},
				emphasis: {
					focus: 'series',
				},
				data: [12, 28, 11, 23, 22],
			},
		],
	};
	state.global.lineLeftTwoChart.setOption(option);
	state.myCharts.push(state.global.lineLeftTwoChart);
};
// 左4
const initPieChart = () => {

	useHomeDeviceModuleSummaryApi().then(({ content }) => {
		stateAll.leftFour = content
	}).then(() => {
		if (!state.global.dispose.some((b: any) => b === state.global.homeChartTwo)) state.global.homeChartTwo.dispose();
		state.global.homeChartTwo = markRaw(echarts.init(homePieRef.value, state.charts.theme));
		var getname = getName(stateAll.leftFour);
		var getvalue = getData(stateAll.leftFour);
		var data = [];
		for (var i = 0; i < getname.length; i++) {
			data.push({ name: getname[i], value: getvalue[i] });
		}
		const option = {
			backgroundColor: state.charts.bgColor,
			title: {
				text: '设备在线情况',
				x: 'left',
				textStyle: { fontSize: '15', color: state.charts.color },
			},
			color: ['#51A3FC', '#36C78B', '#FEC279', '#968AF5', '#E790E8'],
			tooltip: {
				trigger: 'item'
			},
			legend: {
				bottom: '0',
				left: 'center',
				data: getname,
			},
			series: [
				{
					itemStyle: {
						borderRadius: 4,
						borderColor: '#fff',
						borderWidth: 2
					},
					emphasis: {
						label: {
							show: true,
							fontSize: 20,
							fontWeight: 'bold'
						}
					},
					type: 'pie',
					radius: ['20', themeConfig.value.isIsDark ? '40' : '55'],
					center: ['50%', '50%'],
					data: data,

				},
			],
		};
		state.global.homeChartTwo.setOption(option);
		state.myCharts.push(state.global.homeChartTwo);
	})


};
// 左3
const initlineLeftFourChart = () => {
	useHomeDeviceStateSummaryApi().then(({ content }) => {
		stateAll.leftThree = content
	}).then(() => {
		if (!state.global.dispose.some((b: any) => b === state.global.lineLeftFourChart)) state.global.lineLeftFourChart.dispose();
		state.global.lineLeftFourChart = markRaw(echarts.init(lineLeftFourChartRef.value, state.charts.theme));
		var getname = getName(stateAll.leftThree);
		var getvalue = getData(stateAll.leftThree);
		var data = [];
		for (var i = 0; i < getname.length; i++) {
			data.push({ name: getname[i], value: getvalue[i] });
		}
		const option = {
			backgroundColor: state.charts.bgColor,
			title: {
				text: '设备管理',
				x: 'left',
				textStyle: { fontSize: '15', color: state.charts.color },
			},
			color: ['#51A3FC', '#36C78B', '#FEC279', '#968AF5', '#E790E8'],
			tooltip: {
				trigger: 'item'
			},
			legend: {
				bottom: '0',
				left: 'center',
				data: getname,
			},
			series: [
				{
					itemStyle: {
						borderRadius: 4,
						borderColor: '#fff',
						borderWidth: 2
					},
					emphasis: {
						label: {
							show: true,
							fontSize: 20,
							fontWeight: 'bold'
						}
					},
					type: 'pie',
					radius: ['20', themeConfig.value.isIsDark ? '40' : '60'],
					center: ['50%', '50%'],
					data: data,

				},
			],
		};
		state.global.lineLeftFourChart.setOption(option);
		state.myCharts.push(state.global.lineLeftFourChart);
	})


};


// 中2
const initLineChart = () => {
	if (!state.global.dispose.some((b: any) => b === state.global.homeChartOne)) state.global.homeChartOne.dispose();
	state.global.homeChartOne = markRaw(echarts.init(homeLineRef.value, state.charts.theme));
	const option = {
		backgroundColor: state.charts.bgColor,
		title: {
			text: '每月考勤统计',
			x: 'left',
			textStyle: { fontSize: '15', color: state.charts.color },
		},
		grid: { top: 70, right: 20, bottom: 30, left: 30 },
		tooltip: { trigger: 'axis' },
		legend: { data: ['正常', '异常'], right: 0 },
		xAxis: {
			data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
		},
		yAxis: [
			{
				type: 'value',
				splitLine: { show: true, lineStyle: { type: 'dashed', color: '#f5f5f5' } },
			},
		],
		series: [
			{
				name: '正常',
				type: 'line',
				symbolSize: 6,
				symbol: 'circle',
				smooth: true,
				data: [30, 41, 30, 65, 53, 53, 53, 41, 30, 65, 53, 20],
				lineStyle: { color: '#16cfd0' },
				itemStyle: { color: 'rgb(22,207,208,.5)', borderColor: '#16cfd0' },
				areaStyle: {
					color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
						{ offset: 0, color: 'rgba(7,165,255,0.2)' },

					]),
				},
			},
			{
				name: '异常',
				type: 'line',
				symbolSize: 6,
				symbol: 'circle',
				smooth: true,
				data: [0, 21, 7, 15, 42, 42, 42, 24, 7, 15, 42, 0],
				lineStyle: { color: 'rgba(7,165,255,0.5)' },
				itemStyle: { color: 'rgba(7,165,255,.5)', borderColor: 'rgba(7,165,255)' },
				areaStyle: {
					color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
						{ offset: 0, color: 'rgba(7,165,255,0.2)' },

					]),
				},
				emphasis: {
					itemStyle: {
						color: {
							type: 'radial',
							x: 0.5,
							y: 0.5,
							r: 0.5,
							colorStops: [
								{ offset: 0, color: '#9E87FF' },
								{ offset: 0.4, color: '#9E87FF' },
								{ offset: 0.5, color: '#fff' },
								{ offset: 0.7, color: '#fff' },
								{ offset: 0.8, color: '#fff' },
								{ offset: 1, color: '#fff' },
							],
						},
						borderColor: '#9E87FF',
						borderWidth: 2,
					},
				},
			},
		],
	};
	state.global.homeChartOne.setOption(option);
	state.myCharts.push(state.global.homeChartOne);
};
// 中3
const initCenterThreeChart = () => {
	if (!state.global.dispose.some((b: any) => b === state.global.lineCenterThreeChart)) state.global.lineCenterThreeChart.dispose();
	state.global.lineCenterThreeChart = markRaw(echarts.init(lineCenterThreeChartRef.value, state.charts.theme));
	const option = {
		title: {
			text: '每月异常事件',
			x: 'left',
			textStyle: { fontSize: '15', color: state.charts.color },
		},
		grid: {
			top: 50,
			right: 0,
			bottom: 50,
			left: 30,
		},
		tooltip: {
			trigger: 'axis',
		},
		xAxis: {
			data: ['1月', '2月', '3月', '4月', '5月', '6月'],
			axisLine: {
				lineStyle: {
					color: 'rgba(22, 207, 208, 0.1)',
					width: 1,
				},
			},
			axisTick: {
				show: false,
			},
			axisLabel: {
				color: '#16cfd0',
			},
		},
		yAxis: [
			{
				type: 'value',
				axisLine: {
					show: true,
					lineStyle: {
						color: 'rgba(22, 207, 208, 0.1)',
					},
				},
				axisLabel: {
					color: '#16cfd0',
				},
				splitLine: {
					show: true,
					lineStyle: {
						color: 'rgba(22, 207, 208, 0.3)',
					},
				},
				splitArea: {
					show: true,
					areaStyle: {
						color: 'rgba(22, 207, 208, 0.02)',
					},
				},
				nameTextStyle: {
					color: '#16cfd0',
				},
			},
		],
		series: [
			{
				name: '异常事件总数',
				type: 'line',
				data: [5, 4, 1, 0, 2, 4],
				itemStyle: {
					color: '#16cfd0',
				},
			},

		],
	};
	state.global.lineCenterThreeChart.setOption(option);
	state.myCharts.push(state.global.lineCenterThreeChart);
};


// 右1
const initLineRightOneChart = () => {

	useHomeAttendPointSummaryApi().then(({ content }) => {
		stateAll.rightTwo = content;
	}).then(() => {
		if (!state.global.dispose.some((b: any) => b === state.global.lineRightOneChart)) state.global.lineRightOneChart.dispose();
		state.global.lineRightOneChart = markRaw(echarts.init(lineRightOneChartRef.value, state.charts.theme));
		let itemArr = Object.keys(stateAll.rightTwo)
		let name = itemArr.map((item: any) => {
			return stateAll.rightTwo[item].name
		})
		let num = itemArr.map((item: any) => {
			return stateAll.rightTwo[item].num
		})
		const min = 0;
		const max = 1000;
		const option = {
			title: {
				text: '考勤人员统计',
				x: 'left',
				textStyle: { fontSize: '15', color: state.charts.color },
			},
			grid: {
				top: 50,
				right: 10,
				bottom: 66,
				left: 38,
			},
			tooltip: {
				trigger: 'axis',
				axisPointer: {
					type: 'shadow',
				},
			},
			xAxis: [
				{
					type: 'value',
					name: '天数',
					nameGap: 25,
					axisLine: {
						show: true,
						lineStyle: {
							color: 'rgba(7, 165, 255, 0.9)',
						},
					},
					axisLabel: {
						color: '#16cfd0',
					},
					splitLine: {
						show: true,
						lineStyle: {
							color: 'rgba(22, 207, 208, 0.3)',
						},
					},
					splitArea: {
						show: true,
						areaStyle: {
							color: 'rgba(22, 207, 208, 0.02)',
						},
					},
					nameTextStyle: {
						color: '#16cfd0',
					},
				},
			],
			yAxis: [
				{
					type: 'category',
					data: name,
					axisLabel: {
						color: '#000',
						textStyle: {
							fontSize: 9,
						},
						interval: 0,
						rotate: -30,
					},
					axisLine: {
						lineStyle: {
							color: 'rgba(7, 165, 255, 0.9)',
							width: 1,
						},
					},
					axisTick: {
						show: false,
					},
				},
			],
			series: [
				{
					type: 'bar',
					itemStyle: {
						normal: {
							color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
								{
									offset: 0,
									color: 'rgba(7,165,255,0.3)',
								},
								{
									offset: 1,
									color: 'rgba(7,165,255,0.5)',
								},
							]),
						},
					},
					label: {
						normal: {
							show: true,
							position: 'right',
							formatter: function (param: any) {
								if (param.value == max || param.value == min) {
									return '';
								}
								return param.value;
							},
							textStyle: {
								color: 'rgba(7, 165, 255, 0.9)',
								fontSize: 10,
							},
						},
					},
					markPoint: {
						symbolSize: 30,
						label: {
							normal: {
								textStyle: {
									color: '#ffffff',
									fontSize: 10,
								},
							},
						},
						data: [
							{ name: '年最低', value: min, xAxis: 0, yAxis: 100 },
							{ name: '年最高', value: max, xAxis: 9, yAxis: 1000 },
						],
					},
					data: num,
				}
			],
		};
		state.global.lineRightOneChart.setOption(option);
		state.myCharts.push(state.global.lineRightOneChart);
	})


};
// 右2
const initLineRightTwoChart = () => {
	useHomePosPointSummaryApi().then(({ content }) => {
		stateAll.rightTwo = content;

	}).then(() => {
		if (!state.global.dispose.some((b: any) => b === state.global.lineRightTwoChart)) state.global.lineRightTwoChart.dispose();
		state.global.lineRightTwoChart = markRaw(echarts.init(lineRightTwoChartRef.value, state.charts.theme));
		let itemArr = Object.keys(stateAll.rightTwo)
		let name = itemArr.map((item: any) => {
			return stateAll.rightTwo[item].name
		})
		let num = itemArr.map((item: any) => {
			return stateAll.rightTwo[item].num
		})
		const min = 0;
		const max = 1000;
		const option = {
			title: {
				text: '消费人员统计',
				x: 'left',
				textStyle: { fontSize: '15', color: state.charts.color },
			},
			grid: {
				top: 50,
				right: 10,
				bottom: 66,
				left: 38,
			},
			tooltip: {
				trigger: 'axis',
				axisPointer: {
					type: 'shadow',
				},
			},
			xAxis: [
				{
					type: 'value',
					name: '天数',
					nameGap: 25,
					axisLine: {
						show: true,
						lineStyle: {
							color: 'rgba(7, 165, 255, 0.9)',
						},
					},
					axisLabel: {
						color: '#16cfd0',
					},
					splitLine: {
						show: true,
						lineStyle: {
							color: 'rgba(22, 207, 208, 0.3)',
						},
					},
					splitArea: {
						show: true,
						areaStyle: {
							color: 'rgba(22, 207, 208, 0.02)',
						},
					},
					nameTextStyle: {
						color: '#16cfd0',
					},
				},
			],
			yAxis: [
				{
					type: 'category',
					data: name,
					axisLabel: {
						color: '#000',
						textStyle: {
							fontSize: 9,
						},
						interval: 0,
						rotate: -30,
					},
					axisLine: {
						lineStyle: {
							color: 'rgba(7, 165, 255, 0.9)',
							width: 1,
						},
					},
					axisTick: {
						show: false,
					},
				},
			],
			series: [
				{
					type: 'bar',
					itemStyle: {
						normal: {
							color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
								{
									offset: 0,
									color: 'rgba(7,165,255,0.3)',
								},
								{
									offset: 1,
									color: 'rgba(7,165,255,0.5)',
								},
							]),
						},
					},
					label: {
						normal: {
							show: true,
							position: 'right',
							formatter: function (param: any) {
								if (param.value == max || param.value == min) {
									return '';
								}
								return param.value;
							},
							textStyle: {
								color: 'rgba(7, 165, 255, 0.9)',
								fontSize: 10,
							},
						},
					},
					markPoint: {
						symbolSize: 30,
						label: {
							normal: {
								textStyle: {
									color: '#ffffff',
									fontSize: 10,
								},
							},
						},
						data: [
							{ name: '年最低', value: min, xAxis: 0, yAxis: 100 },
							{ name: '年最高', value: max, xAxis: 9, yAxis: 1000 },
						],
					},
					data: num,
				}
			],
		};
		state.global.lineRightTwoChart.setOption(option);
		state.myCharts.push(state.global.lineRightTwoChart);
	})


};
// 右3
const initLineRightThreeChart = () => {
	useHomeAccessInoutSummaryApi().then(({ content }) => {
		stateAll.rightThree = content;

	}).then(() => {
		if (!state.global.dispose.some((b: any) => b === state.global.lineRightThreeChart)) state.global.lineRightThreeChart.dispose();
		state.global.lineRightThreeChart = markRaw(echarts.init(lineRightThreeChartRef.value, state.charts.theme));
		let itemArr = Object.keys(stateAll.rightThree)
		let name = itemArr.map((item: any) => {
			return stateAll.rightThree[item].name
		})
		let num = itemArr.map((item: any) => {
			return stateAll.rightThree[item].num
		})
		const min = 0;
		const max = 1000;
		const option = {
			title: {
				text: '进出人员统计',
				x: 'left',
				textStyle: { fontSize: '15', color: state.charts.color },
			},
			grid: {
				top: 50,
				right: 10,
				bottom: 66,
				left: 38,
			},
			tooltip: {
				trigger: 'axis',
				axisPointer: {
					type: 'shadow',
				},
			},
			xAxis: [
				{
					type: 'value',
					name: '天数',
					nameGap: 25,
					axisLine: {
						show: true,
						lineStyle: {
							color: 'rgba(7, 165, 255, 0.9)',
						},
					},
					axisLabel: {
						color: '#16cfd0',
					},
					splitLine: {
						show: true,
						lineStyle: {
							color: 'rgba(22, 207, 208, 0.3)',
						},
					},
					splitArea: {
						show: true,
						areaStyle: {
							color: 'rgba(22, 207, 208, 0.02)',
						},
					},
					nameTextStyle: {
						color: '#16cfd0',
					},
				},
			],
			yAxis: [
				{
					type: 'category',
					data: name,
					axisLabel: {
						color: '#000',
						textStyle: {
							fontSize: 9,
						},
						interval: 0,
						rotate: -30,
					},
					axisLine: {
						lineStyle: {
							color: 'rgba(7, 165, 255, 0.9)',
							width: 1,
						},
					},
					axisTick: {
						show: false,
					},
				},
			],
			series: [
				{
					type: 'bar',
					itemStyle: {
						normal: {
							color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
								{
									offset: 0,
									color: 'rgba(7,165,255,0.3)',
								},
								{
									offset: 1,
									color: 'rgba(7,165,255,0.5)',
								},
							]),
						},
					},
					label: {
						normal: {
							show: true,
							position: 'right',
							formatter: function (param: any) {
								if (param.value == max || param.value == min) {
									return '';
								}
								return param.value;
							},
							textStyle: {
								color: 'rgba(7, 165, 255, 0.9)',
								fontSize: 10,
							},
						},
					},
					markPoint: {
						symbolSize: 30,
						label: {
							normal: {
								textStyle: {
									color: '#ffffff',
									fontSize: 10,
								},
							},
						},
						data: [
							{ name: '年最低', value: min, xAxis: 0, yAxis: 100 },
							{ name: '年最高', value: max, xAxis: 9, yAxis: 1000 },
						],
					},
					data: num,
				}
			],
		};
		state.global.lineRightThreeChart.setOption(option);
		state.myCharts.push(state.global.lineRightThreeChart);
	})


};

const initAttendYesterdaySummary = () => {
	useHomeAttendYesterdaySummaryApi().then(({ content }) => {
		state.homeThree.forEach((card: any) => {
			Object.keys(content).forEach((item: any) => {
				if (card.title === item) {
					card.value = content[item]
				}
			})
		})
	})
}

const getAllAttendPoints = () => {
	useHomeAttendPointSummaryApi().then(({ content }) => {
		stateAll.rightOne = content;
		let itemArr = Object.keys(stateAll.rightOne)
		let name = itemArr.map((item: any) => {
			return stateAll.rightOne[item].name
		})
		let num = itemArr.map((item: any) => {
			return stateAll.rightOne[item].num
		})
	})
}

const getAllPosPoints = () => {
	useHomePosPointSummaryApi().then(({ content }) => {
		stateAll.rightTwo = content;
		let itemArr = Object.keys(stateAll.rightOne)
		let name = itemArr.map((item: any) => {
			return stateAll.rightOne[item].name
		})
		let num = itemArr.map((item: any) => {
			return stateAll.rightOne[item].num
		})
	})
}

const getAccessInout = () => {
	useHomeAccessInoutSummaryApi().then(({ content }) => {
		stateAll.rightThree = content;
		let itemArr = Object.keys(stateAll.rightOne)
		let name = itemArr.map((item: any) => {
			return stateAll.rightOne[item].name
		})
		let num = itemArr.map((item: any) => {
			return stateAll.rightOne[item].num
		})
	})
}


// 批量设置 echarts resize
const initEchartsResizeFun = () => {
	nextTick(() => {
		for (let i = 0; i < state.myCharts.length; i++) {
			setTimeout(() => {
				state.myCharts[i].resize();
			}, i * 1000);
		}
	});
};
// 批量设置 echarts resize
const initEchartsResize = () => {
	window.addEventListener('resize', initEchartsResizeFun);
};
// 页面加载时
onMounted(() => {
	initEchartsResize();
	initLeftOne();
	initAttendYesterdaySummary()
	getAllAttendPoints()
	getAllPosPoints()
	getAccessInout()
});
// 由于页面缓存原因，keep-alive
onActivated(() => {
	initEchartsResizeFun();

});
// 监听 pinia 中的 tagsview 开启全屏变化，重新 resize 图表，防止不出现/大小不变等
watch(
	() => isTagsViewCurrenFull.value,
	() => {
		initEchartsResizeFun();
	}
);
// 监听 pinia 中是否开启深色主题
watch(
	() => themeConfig.value.isIsDark,
	(isIsDark) => {
		nextTick(() => {
			state.charts.theme = isIsDark ? 'dark' : '';
			state.charts.bgColor = isIsDark ? 'transparent' : '';
			state.charts.color = isIsDark ? '#dadada' : '#303133';
			setTimeout(() => {
				initLineChart();
			}, 500);
			setTimeout(() => {
				initPieChart();
			}, 700);
			setTimeout(() => {
				initCenterThreeChart();
			}, 1000);
			setTimeout(() => {
				initLineRightOneChart();
			}, 1200);
			setTimeout(() => {
				initLineRightTwoChart();
			}, 1300);
			setTimeout(() => {
				initLineRightThreeChart();
			}, 1400);
			setTimeout(() => {
				initLineLeftTwoChart();
			}, 400);
			setTimeout(() => {
				initlineLeftFourChart();
			}, 300);
		});
	},
	{
		deep: true,
		immediate: true,
	}
);
</script>

<style scoped lang="scss">
$homeNavLengh: 8;

.home-container {
	overflow: hidden;

	.home-card-one,
	.home-card-two,
	.home-card-three {
		.home-card-item {
			width: 100%;
			height: 130px;
			border-radius: 4px;
			transition: all ease 0.3s;
			padding: 20px;
			overflow: hidden;
			background: var(--el-color-white);
			color: var(--el-text-color-primary);
			border: 1px solid var(--next-border-color-light);

			&:hover {
				box-shadow: 0 2px 12px var(--next-color-dark-hover);
				transition: all ease 0.3s;
			}

			&-icon {
				width: 70px;
				height: 70px;
				border-radius: 100%;
				flex-shrink: 1;

				i {
					color: var(--el-text-color-placeholder);
				}
			}

			&-title {
				font-size: 15px;
				font-weight: bold;
				height: 30px;
			}
		}
	}

	.home-card-one {
		@for $i from 0 through 3 {
			.home-one-animation#{$i} {
				opacity: 0;
				animation-name: error-num;
				animation-duration: 0.5s;
				animation-fill-mode: forwards;
				animation-delay: calc($i/4) + s;
			}
		}
	}

	.home-card-two,
	.home-card-three {
		.home-card-item {
			height: 400px;
			width: 100%;
			overflow: hidden;

			.home-monitor {
				height: 100%;

				.flex-warp-item {
					width: 25%;
					height: 111px;
					display: flex;

					.flex-warp-item-box {
						margin: auto;
						text-align: center;
						color: var(--el-text-color-primary);
						display: flex;
						border-radius: 5px;
						// background: var(--next-bg-color);
						background: linear-gradient(to right, #eff3f5 0%, #ddecf5 100%);
						cursor: pointer;
						transition: all 0.3s ease;

						&:hover {
							background: var(--el-color-primary-light-9);

							transition: all 0.3s ease;
						}
					}

					@for $i from 0 through $homeNavLengh {
						.home-animation#{$i} {
							opacity: 0;
							animation-name: error-num;
							animation-duration: 0.5s;
							animation-fill-mode: forwards;
							animation-delay: calc($i/10) + s;
						}
					}
				}
			}
		}
	}
}

.c-title {
	font-size: 16px;
}

.c-content {
	font-size: 18px;
	font-weight: bold;
	transition: all .3s;
}

.flex-warp-item:hover .c-content {
	transform: scale(1.2);
}
</style>
